﻿using System;

namespace Aocsoft
{
    public static class LoggerContext
    {
        static ILoggerFactory _LoggerFactory;
        static object _lockObject = new object();
        public static ILoggerFactory LoggerFactory
        {
            get
            {
                if (_LoggerFactory == null)
                {
                    lock (_lockObject)
                    {
                        if (_LoggerFactory == null)
                        {
                            _LoggerFactory = InitLoggerFactory();
                        }
                    }
                }
                return _LoggerFactory;
            }
        }
        static ILoggerFactory InitLoggerFactory()
        {
            string loggerFactoryTypeString = System.Configuration.ConfigurationManager.AppSettings["aocsoft-logger"];
            if (!string.IsNullOrWhiteSpace(loggerFactoryTypeString))
            {
                var obj = Activator.CreateInstance(Type.GetType(loggerFactoryTypeString));
                if (obj is ILoggerFactory) return obj as ILoggerFactory;
            }
            return new NullLoggerFactory();
        }
    }

    public class NullLoggerFactory : LoggerFactoryBase, ILoggerFactory
    {
        public ILogger LoggerFor(string keyName)
        {
            return new NullINternalLogger();
        }

        public ILogger LoggerFor(Type type)
        {
            return new NullINternalLogger();
        }
    }

    public class NullINternalLogger : ILogger
    {

        public bool IsDebugEnabled
        {
            get { return false; }
        }

        public bool IsErrorEnabled
        {
            get { return false; }
        }

        public bool IsFatalEnabled
        {
            get { return false; }
        }

        public bool IsInfoEnabled
        {
            get { return false; }
        }

        public bool IsWarnEnabled
        {
            get { return false; }
        }

        public void Debug(object message)
        {

        }

        public void Debug(object message, Exception exception)
        {

        }

        public void DebugFormat(string format, params object[] args)
        {

        }

        public void Error(object message)
        {

        }

        public void Error(object message, Exception exception)
        {

        }

        public void ErrorFormat(string format, params object[] args)
        {
        }

        public void Fatal(object message)
        {

        }

        public void Fatal(object message, Exception exception)
        {

        }

        public void Info(object message)
        {

        }

        public void Info(object message, Exception exception)
        {

        }

        public void InfoFormat(string format, params object[] args)
        {

        }

        public void Warn(object message)
        {

        }

        public void Warn(object message, Exception exception)
        {

        }

        public void WarnFormat(string format, params object[] args)
        {

        }

        public void Trace(object message)
        {

        }

        public void Trace(object message, Exception exception)
        {

        }

        public void TraceFormat(string format, params object[] args)
        {

        }


        public void FatalFormat(string format, params object[] args)
        {
            
        }
    }
}
